using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_SaleOrderEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        SaleOrder saleOrder;

        if (Int32.TryParse(Request["id"], out id))
            saleOrder = SaleOrder.Load(id);
        else
            saleOrder = null;

        if (saleOrder != null)
        {
            tbxOrderedBySiteuserId.Text = saleOrder.OrderedBySiteuserId.ToString();
			tbxItemProductId.Text = saleOrder.ItemProductId.ToString();
			tbxItemQuantity.Text = saleOrder.ItemQuantity.ToString();
			tbxTotalPrice.Text = saleOrder.TotalPrice.ToString();
			tbxGiftCouponUseAmount.Text = saleOrder.GiftCouponUseAmount.ToString();
			tbxTax.Text = saleOrder.Tax.ToString();
			tbxShippingCost.Text = saleOrder.ShippingCost.ToString();
			tbxFinalAmount.Text = saleOrder.FinalAmount.ToString();
			cbxIsPaymentClear.Checked = saleOrder.IsPaymentClear;
			if (!string.IsNullOrEmpty(saleOrder.TrackingNumber))
				tbxTrackingNumber.Text = saleOrder.TrackingNumber;
			if (!string.IsNullOrEmpty(saleOrder.OrderStatus))
				tbxOrderStatus.Text = saleOrder.OrderStatus;

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        int orderedBySiteuserId = Security.BindIntegerInput(tbxOrderedBySiteuserId.Text, _securityLevel);
		int itemProductId = Security.BindIntegerInput(tbxItemProductId.Text, _securityLevel);
		int itemQuantity = Security.BindIntegerInput(tbxItemQuantity.Text, _securityLevel);
		double totalPrice = Security.BindDoubleInput(tbxTotalPrice.Text, _securityLevel);
		double giftCouponUseAmount = Security.BindDoubleInput(tbxGiftCouponUseAmount.Text, _securityLevel);
		double tax = Security.BindDoubleInput(tbxTax.Text, _securityLevel);
		double shippingCost = Security.BindDoubleInput(tbxShippingCost.Text, _securityLevel);
		double finalAmount = Security.BindDoubleInput(tbxFinalAmount.Text, _securityLevel);
		bool isPaymentClear = cbxIsPaymentClear.Checked;
		string trackingNumber = Security.BindStringInput(tbxTrackingNumber.Text, _securityLevel);
		string orderStatus = Security.BindStringInput(tbxOrderStatus.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (SaleOrder.Save(
				id, 
				orderedBySiteuserId,
				itemProductId,
				itemQuantity,
				totalPrice,
				giftCouponUseAmount,
				tax,
				shippingCost,
				finalAmount,
				isPaymentClear,
				trackingNumber,
				orderStatus
                ))
                    Response.Redirect("saleorderlist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            SaleOrder saleOrder = SaleOrder.Create(
				orderedBySiteuserId,
				itemProductId,
				itemQuantity,
				totalPrice,
				giftCouponUseAmount,
				tax,
				shippingCost,
				finalAmount,
				isPaymentClear,
				trackingNumber,
				orderStatus
                );

            if (saleOrder != null)
                Response.Redirect("saleorderlist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("saleorderlist.aspx");
    }
}
